package com.shan.mybatis.controllor;

import com.shan.mybatis.anno.Log;
import com.shan.mybatis.pojo.Emp;
import com.shan.mybatis.pojo.PageBean;
import com.shan.mybatis.pojo.Result;
import com.shan.mybatis.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * @Author: ShanYuqing
 * @Version: v1.0
 */
@RestController
public class EmpControllor {
    @Autowired
    private EmpService service;

    //    @GetMapping("/emps")
//    public Result page(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize){
//      PageBean pageBean = service.page(page,pageSize);
//      return Result.success(pageBean);
//    }
    @GetMapping("/emps")
    public Result query(String name, Short gender,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                        @RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "10") Integer pageSize) {
        PageBean pageBean = service.query(name, gender, begin, end, page, pageSize);

        return Result.success(pageBean);
    }
    @Log
    @DeleteMapping("/emps/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids) {
        service.deleteByIds(ids);
        return Result.success();
    }
    @Log
    @PostMapping("/emps")
    public Result addEmp(@RequestBody Emp emp) {
        service.addEmp(emp);
        return Result.success();
    }

    @GetMapping("/emps/{id}")
    public Result queryById(@PathVariable Integer id) {
        Emp emp = service.queryById(id);
        return Result.success(emp);
    }
    @Log
    @PutMapping("/emps")
    public Result update(@RequestBody Emp emp){
        service.update(emp);
        return Result.success();
    }
}
